/*     */ package com.zimbra.cs.util.tnef;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.util.tnef.mapi.BusyStatus;
/*     */ import com.zimbra.cs.util.tnef.mapi.ChangedInstanceInfo;
/*     */ import com.zimbra.cs.util.tnef.mapi.ExceptionInfoOverrideFlag;
/*     */ import com.zimbra.cs.util.tnef.mapi.MapiPropertyId;
/*     */ import com.zimbra.cs.util.tnef.mapi.MeetingTypeFlag;
/*     */ import com.zimbra.cs.util.tnef.mapi.RecurrenceDefinition;
/*     */ import com.zimbra.cs.util.tnef.mapi.TaskMode;
/*     */ import com.zimbra.cs.util.tnef.mapi.TaskStatus;
/*     */ import com.zimbra.cs.util.tnef.mapi.TimeZoneDefinition;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.net.URISyntaxException;
/*     */ import java.text.ParseException;
/*     */ import java.util.EnumSet;
/*     */ import java.util.List;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.Message.RecipientType;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import net.fortuna.ical4j.data.ContentHandler;
/*     */ import net.fortuna.ical4j.data.ParserException;
/*     */ import net.fortuna.ical4j.model.CategoryList;
/*     */ import net.fortuna.ical4j.model.DateTime;
/*     */ import net.fortuna.ical4j.model.ParameterList;
/*     */ import net.fortuna.ical4j.model.Property;
/*     */ import net.fortuna.ical4j.model.parameter.Cn;
/*     */ import net.fortuna.ical4j.model.parameter.CuType;
/*     */ import net.fortuna.ical4j.model.parameter.PartStat;
/*     */ import net.fortuna.ical4j.model.parameter.Related;
/*     */ import net.fortuna.ical4j.model.parameter.Role;
/*     */ import net.fortuna.ical4j.model.parameter.Rsvp;
/*     */ import net.fortuna.ical4j.model.parameter.SentBy;
/*     */ import net.fortuna.ical4j.model.property.Action;
/*     */ import net.fortuna.ical4j.model.property.Attendee;
/*     */ import net.fortuna.ical4j.model.property.CalScale;
/*     */ import net.fortuna.ical4j.model.property.Categories;
/*     */ import net.fortuna.ical4j.model.property.Clazz;
/*     */ import net.fortuna.ical4j.model.property.DtStamp;
/*     */ import net.fortuna.ical4j.model.property.Method;
/*     */ import net.fortuna.ical4j.model.property.Organizer;
/*     */ import net.fortuna.ical4j.model.property.Priority;
/*     */ import net.fortuna.ical4j.model.property.Status;
/*     */ import net.fortuna.ical4j.model.property.Transp;
/*     */ import net.fortuna.ical4j.model.property.Trigger;
/*     */ import net.fortuna.ical4j.model.property.XProperty;
/*     */ import net.freeutils.tnef.Attachment;
/*     */ import net.freeutils.tnef.MAPIProp;
/*     */ import net.freeutils.tnef.MAPIProps;
/*     */ import net.freeutils.tnef.TNEFInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DefaultTnefToICalendar
/*     */   implements TnefToICalendar
/*     */ {
/*  89 */   static Log sLog = ZimbraLog.tnef;
/*     */   
/*     */   private RecurrenceDefinition recurDef;
/*     */   
/*     */   private DtStamp dtstamp;
/*     */   
/*     */   private String uid;
/*     */   
/*     */   private Method method;
/*     */   private ICALENDAR_TYPE icalType;
/*     */   
/*     */   public boolean convert(MimeMessage mimeMsg, InputStream tnefInput, ContentHandler icalOutput)
/*     */     throws ServiceException
/*     */   {
/* 103 */     conversionSuccessful = false;
/* 104 */     this.recurDef = null;
/* 105 */     TNEFInputStream tnefStream = null;
/* 106 */     SchedulingViewOfTnef schedView = null;
/* 107 */     Integer sequenceNum = Integer.valueOf(0);
/* 108 */     this.icalType = ICALENDAR_TYPE.VEVENT;
/*     */     try
/*     */     {
/* 111 */       tnefStream = new TNEFInputStream(tnefInput);
/* 112 */       schedView = new SchedulingViewOfTnef(tnefStream);
/*     */       
/* 114 */       String msgClass = schedView.getMessageClass();
/* 115 */       if (msgClass == null) {
/* 116 */         sLog.debug("Unable to determine Class of TNEF - cannot generate ICALENDER equivalent");
/*     */         
/* 118 */         return false;
/*     */       }
/* 120 */       this.icalType = schedView.getIcalType();
/* 121 */       this.method = null;
/* 122 */       PartStat partstat = null;
/* 123 */       boolean replyWanted = schedView.getResponseRequested();
/* 124 */       Boolean isCounterProposal = schedView.isCounterProposal();
/* 125 */       if (msgClass != null)
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 136 */         if (msgClass.startsWith("IPM.Microsoft Schedule.MtgReq")) {
/* 137 */           this.method = Method.REQUEST;
/* 138 */           partstat = PartStat.NEEDS_ACTION;
/* 139 */         } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgRespP")) {
/* 140 */           this.method = Method.REPLY;
/* 141 */           partstat = PartStat.ACCEPTED;
/* 142 */           replyWanted = false;
/* 143 */         } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgRespN")) {
/* 144 */           this.method = Method.REPLY;
/* 145 */           partstat = PartStat.DECLINED;
/* 146 */           replyWanted = false;
/* 147 */         } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgRespA")) {
/* 148 */           if ((isCounterProposal != null) && (isCounterProposal.booleanValue())) {
/* 149 */             this.method = Method.COUNTER;
/*     */           } else {
/* 151 */             this.method = Method.REPLY;
/*     */           }
/* 153 */           partstat = PartStat.TENTATIVE;
/* 154 */           replyWanted = false;
/* 155 */         } else if (msgClass.startsWith("IPM.Microsoft Schedule.MtgCncl")) {
/* 156 */           this.method = Method.CANCEL;
/* 157 */           replyWanted = false;
/* 158 */         } else if (msgClass.startsWith("IPM.TaskRequest.Accept")) {
/* 159 */           this.method = Method.REPLY;
/* 160 */           partstat = PartStat.ACCEPTED;
/* 161 */           replyWanted = false;
/* 162 */         } else if (msgClass.startsWith("IPM.TaskRequest.Decline")) {
/* 163 */           this.method = Method.REPLY;
/* 164 */           partstat = PartStat.DECLINED;
/* 165 */           replyWanted = false;
/* 166 */         } else if (msgClass.startsWith("IPM.TaskRequest.Update")) {
/* 167 */           this.method = Method.REPLY;
/* 168 */           partstat = PartStat.IN_PROCESS;
/* 169 */           replyWanted = false;
/* 170 */         } else if (msgClass.startsWith("IPM.TaskRequest")) {
/* 171 */           this.method = Method.REQUEST;
/* 172 */           partstat = PartStat.NEEDS_ACTION;
/* 173 */           replyWanted = true;
/*     */         }
/*     */       }
/*     */       
/* 177 */       if (this.method == null) {
/* 178 */         sLog.debug("Unable to map class %s to ICALENDER", new Object[] { msgClass });
/* 179 */         return false;
/*     */       }
/*     */       
/*     */ 
/* 183 */       if (this.icalType == ICALENDAR_TYPE.VTODO) {
/* 184 */         Object attaches = schedView.getAttachments();
/* 185 */         if (attaches == null) {
/* 186 */           sLog.debug("Unable to map class %s to ICALENDER - no attachments", new Object[] { msgClass });
/* 187 */           return 0;
/*     */         }
/* 189 */         schedView = null;
/* 190 */         for (Object obj : (List)attaches) {
/* 191 */           if ((obj instanceof Attachment)) {
/* 192 */             Attachment currAttach = (Attachment)obj;
/* 193 */             MAPIProps attachMPs = currAttach.getMAPIProps();
/* 194 */             if (attachMPs != null) {
/* 195 */               MAPIProp attachData = attachMPs.getProp(14081);
/* 196 */               if (attachData != null) {
/* 197 */                 Object theVal = attachData.getValue();
/* 198 */                 if ((theVal != null) && ((theVal instanceof TNEFInputStream))) {
/* 199 */                   TNEFInputStream tnefSubStream = (TNEFInputStream)theVal;
/* 200 */                   schedView = new SchedulingViewOfTnef(tnefSubStream);
/* 201 */                   break;
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/* 207 */         if (schedView == null) {
/* 208 */           sLog.debug("Unable to map class %s to ICALENDER - no properties found for sub-msg", new Object[] { msgClass });
/* 209 */           return 0;
/*     */         }
/*     */       }
/* 212 */       this.uid = schedView.getIcalUID();
/* 213 */       sequenceNum = schedView.getSequenceNumber();
/* 214 */       boolean reminderSet = schedView.getReminderSet();
/* 215 */       String location = schedView.getLocation();
/* 216 */       Boolean isAllDayEvent = schedView.isAllDayEvent();
/* 217 */       Integer importance = schedView.getMapiImportance();
/* 218 */       Clazz icalClass = schedView.getIcalClass();
/* 219 */       Integer ownerApptId = schedView.getOwnerAppointmentId();
/* 220 */       EnumSet<MeetingTypeFlag> meetingTypeFlags = schedView.getMeetingTypeFlags();
/* 221 */       BusyStatus busyStatus = schedView.getBusyStatus();
/* 222 */       BusyStatus intendedBusyStatus = schedView.getIntendedBusyStatus();
/*     */       
/*     */ 
/* 225 */       BusyStatus bestBusyStatus = intendedBusyStatus;
/* 226 */       if (bestBusyStatus == null) {
/* 227 */         bestBusyStatus = busyStatus;
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 232 */       TimeZoneDefinition startTimeTZinfo = schedView.getStartDateTimezoneInfo();
/* 233 */       TimeZoneDefinition endTimeTZinfo = schedView.getEndDateTimezoneInfo();
/* 234 */       TimeZoneDefinition recurrenceTZinfo = schedView.getRecurrenceTimezoneInfo();
/* 235 */       this.recurDef = schedView.getRecurrenceDefinition(recurrenceTZinfo);
/*     */       
/* 237 */       DateTime icalStartDate = schedView.getStartTime();
/* 238 */       DateTime icalEndDate = schedView.getEndTime();
/* 239 */       DateTime icalDueDate = schedView.getDueDate();
/* 240 */       DateTime icalDateTaskCompleted = schedView.getDateTaskCompleted();
/* 241 */       DateTime icalCreateDate = MapiPropertyId.PidTagCreationTime.getDateTimeAsUTC(schedView);
/*     */       
/* 243 */       DateTime icalLastModDate = MapiPropertyId.PidTagLastModificationTime.getDateTimeAsUTC(schedView);
/*     */       
/* 245 */       DateTime recurrenceIdDateTime = schedView.getRecurrenceIdTime();
/* 246 */       DateTime attendeeCriticalChange = schedView.getAttendeeCriticalChange();
/* 247 */       DateTime ownerCriticalChange = schedView.getOwnerCriticalChange();
/* 248 */       int percentComplete = schedView.getPercentComplete();
/* 249 */       TaskStatus taskStatus = schedView.getTaskStatus();
/* 250 */       TaskMode taskMode = schedView.getTaskMode();
/* 251 */       String mileage = schedView.getMileage();
/* 252 */       String billingInfo = schedView.getBillingInfo();
/* 253 */       String companies = schedView.getCompanies();
/* 254 */       Integer actualEffort = schedView.getActualEffort();
/* 255 */       Integer estimatedEffort = schedView.getEstimatedEffort();
/* 256 */       List<String> categories = schedView.getCategories();
/* 257 */       String descriptionText = null;
/* 258 */       String summary = null;
/* 259 */       if (mimeMsg != null) {
/* 260 */         summary = mimeMsg.getSubject();
/* 261 */         PlainTextFinder finder = new PlainTextFinder();
/* 262 */         finder.accept(mimeMsg);
/* 263 */         descriptionText = finder.getPlainText();
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 269 */       icalOutput.startCalendar();
/*     */       
/*     */ 
/*     */ 
/* 273 */       IcalUtil.addProperty(icalOutput, this.method);
/* 274 */       if (this.recurDef != null) {
/* 275 */         String MsCalScale = this.recurDef.xMicrosoftCalscale();
/* 276 */         if ((MsCalScale == null) || (MsCalScale.equals(""))) {
/* 277 */           IcalUtil.addProperty(icalOutput, CalScale.GREGORIAN);
/*     */         } else {
/* 279 */           IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CALSCALE", MsCalScale);
/*     */         }
/*     */       } else {
/* 282 */         IcalUtil.addProperty(icalOutput, CalScale.GREGORIAN);
/*     */       }
/* 284 */       String startTZname = null;
/* 285 */       String endTZname = null;
/* 286 */       String recurTZname = null;
/* 287 */       if (startTimeTZinfo != null) {
/* 288 */         startTZname = startTimeTZinfo.getTimezoneName();
/* 289 */         startTimeTZinfo.addVtimezone(icalOutput);
/*     */       }
/* 291 */       if (endTimeTZinfo != null) {
/* 292 */         endTZname = endTimeTZinfo.getTimezoneName();
/* 293 */         if ((startTZname == null) || (!endTZname.equals(startTZname))) {
/* 294 */           endTimeTZinfo.addVtimezone(icalOutput);
/*     */         }
/*     */       }
/*     */       boolean addName;
/* 298 */       if (recurrenceTZinfo != null) {
/* 299 */         recurTZname = recurrenceTZinfo.getTimezoneName();
/* 300 */         addName = true;
/* 301 */         if ((startTZname != null) && (recurTZname.equals(startTZname))) {
/* 302 */           addName = false;
/*     */         }
/* 304 */         if ((endTZname != null) && (recurTZname.equals(endTZname))) {
/* 305 */           addName = false;
/*     */         }
/* 307 */         if (addName) {
/* 308 */           recurrenceTZinfo.addVtimezone(icalOutput);
/*     */         }
/*     */       }
/*     */       
/* 312 */       if (this.uid == null) {
/* 313 */         sLog.debug("Unable to map class %s to ICALENDER - no suitable value found for UID", new Object[] { msgClass });
/* 314 */         return false;
/*     */       }
/* 316 */       icalOutput.startComponent(this.icalType.toString());
/*     */       
/* 318 */       IcalUtil.addProperty(icalOutput, "UID", this.uid);
/* 319 */       if ((attendeeCriticalChange != null) && ((this.method.equals(Method.REPLY)) || (this.method.equals(Method.COUNTER))))
/*     */       {
/*     */ 
/* 322 */         this.dtstamp = new DtStamp(attendeeCriticalChange);
/* 323 */       } else if (ownerCriticalChange != null) {
/* 324 */         this.dtstamp = new DtStamp(ownerCriticalChange);
/*     */       } else {
/* 326 */         DateTime stampTime = new DateTime("20000101T000000Z");
/* 327 */         this.dtstamp = new DtStamp(stampTime);
/*     */       }
/* 329 */       IcalUtil.addProperty(icalOutput, this.dtstamp);
/* 330 */       IcalUtil.addProperty(icalOutput, "CREATED", icalCreateDate, false);
/* 331 */       IcalUtil.addProperty(icalOutput, "LAST-MODIFIED", icalLastModDate, false);
/* 332 */       IcalUtil.addProperty(icalOutput, "SEQUENCE", sequenceNum, false);
/* 333 */       if ((summary == null) || (summary.length() == 0))
/*     */       {
/* 335 */         if (this.icalType == ICALENDAR_TYPE.VTODO) {
/* 336 */           if (this.method.equals(Method.REQUEST)) {
/* 337 */             summary = new String("Task Request");
/* 338 */           } else if (this.method.equals(Method.REPLY)) {
/* 339 */             summary = new String("Task Response");
/*     */           } else {
/* 341 */             summary = new String("Task");
/*     */           }
/*     */         }
/* 344 */         else if (this.method.equals(Method.REPLY)) {
/* 345 */           summary = new String("Response");
/* 346 */         } else if (this.method.equals(Method.CANCEL)) {
/* 347 */           summary = new String("Canceled");
/* 348 */         } else if (this.method.equals(Method.COUNTER)) {
/* 349 */           summary = new String("Counter Proposal");
/*     */         }
/*     */       }
/*     */       
/* 353 */       IcalUtil.addProperty(icalOutput, "SUMMARY", summary, false);
/* 354 */       IcalUtil.addProperty(icalOutput, "LOCATION", location, false);
/* 355 */       IcalUtil.addProperty(icalOutput, "DESCRIPTION", descriptionText, false);
/* 356 */       if (this.method.equals(Method.COUNTER)) {
/* 357 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "DTSTART", schedView.getProposedStartTime(), startTimeTZinfo, isAllDayEvent.booleanValue());
/*     */         
/* 359 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "DTEND", schedView.getProposedEndTime(), endTimeTZinfo, isAllDayEvent.booleanValue());
/*     */         
/* 361 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "X-MS-OLK-ORIGINALSTART", icalStartDate, startTimeTZinfo, isAllDayEvent.booleanValue());
/*     */         
/*     */ 
/* 364 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "X-MS-OLK-ORIGINALEND", icalEndDate, endTimeTZinfo, isAllDayEvent.booleanValue());
/*     */ 
/*     */ 
/*     */       }
/* 368 */       else if (this.icalType == ICALENDAR_TYPE.VTODO) {
/* 369 */         IcalUtil.addFloatingDateProperty(icalOutput, "DTSTART", icalStartDate);
/* 370 */         IcalUtil.addFloatingDateProperty(icalOutput, "DUE", icalDueDate);
/* 371 */         Status icalStatus = null;
/* 372 */         if (this.method.equals(Method.CANCEL)) {
/* 373 */           icalStatus = Status.VTODO_CANCELLED;
/* 374 */         } else if (taskStatus != null) {
/* 375 */           if (taskStatus.equals(TaskStatus.COMPLETE)) {
/* 376 */             icalStatus = Status.VTODO_COMPLETED;
/* 377 */           } else if (taskStatus.equals(TaskStatus.IN_PROGRESS)) {
/* 378 */             icalStatus = Status.VTODO_IN_PROCESS;
/*     */           }
/*     */         }
/* 381 */         IcalUtil.addProperty(icalOutput, icalStatus);
/*     */         
/* 383 */         if (percentComplete != 0) {
/* 384 */           IcalUtil.addProperty(icalOutput, "PERCENT-COMPLETE", Integer.valueOf(percentComplete), false);
/*     */         }
/*     */         
/*     */ 
/* 388 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "COMPLETED", icalDateTaskCompleted, null, false);
/*     */       }
/*     */       else {
/* 391 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "DTSTART", icalStartDate, startTimeTZinfo, isAllDayEvent.booleanValue());
/*     */         
/* 393 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "DTEND", icalEndDate, endTimeTZinfo, isAllDayEvent.booleanValue());
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 400 */       if (recurrenceIdDateTime != null) {
/* 401 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "RECURRENCE-ID", recurrenceIdDateTime, startTimeTZinfo, isAllDayEvent.booleanValue());
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 406 */         addRecurrenceRelatedProps(icalOutput, this.recurDef, startTimeTZinfo, isAllDayEvent.booleanValue());
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 411 */       Priority priority = Priority.MEDIUM;
/* 412 */       if (importance != null) {
/* 413 */         if (importance.intValue() == 2) {
/* 414 */           priority = Priority.HIGH;
/* 415 */         } else if (importance.intValue() == 1) {
/* 416 */           priority = Priority.MEDIUM;
/* 417 */         } else if (importance.intValue() == 0) {
/* 418 */           priority = Priority.LOW;
/*     */         }
/*     */       }
/* 421 */       IcalUtil.addProperty(icalOutput, priority);
/*     */       
/* 423 */       IcalUtil.addProperty(icalOutput, icalClass);
/* 424 */       addStatusProperty(icalOutput, bestBusyStatus, meetingTypeFlags);
/* 425 */       addTranspProperty(icalOutput, bestBusyStatus);
/* 426 */       addAttendees(icalOutput, mimeMsg, partstat, replyWanted);
/*     */       
/*     */ 
/*     */ 
/* 430 */       if (categories != null) {
/* 431 */         CategoryList cl = new CategoryList();
/* 432 */         for (String category : categories) {
/* 433 */           cl.add(category);
/*     */         }
/* 435 */         if (cl.size() > 0) {
/* 436 */           Categories myCategories = new Categories(cl);
/* 437 */           IcalUtil.addProperty(icalOutput, myCategories);
/*     */         }
/*     */       }
/* 440 */       if ((taskStatus != null) && (
/* 441 */         (taskStatus.equals(TaskStatus.DEFERRED)) || (taskStatus.equals(TaskStatus.WAITING_ON_OTHER))))
/*     */       {
/* 443 */         IcalUtil.addProperty(icalOutput, "X-ZIMBRA-TASK-STATUS", taskStatus, false);
/*     */       }
/*     */       
/*     */ 
/* 447 */       if ((taskMode != null) && (!taskMode.equals(TaskMode.TASK_REQUEST))) {
/* 448 */         IcalUtil.addProperty(icalOutput, "X-ZIMBRA-TASK-MODE", taskMode, false);
/*     */       }
/*     */       
/* 451 */       IcalUtil.addProperty(icalOutput, "X-ZIMBRA-MILEAGE", mileage, false);
/*     */       
/* 453 */       IcalUtil.addProperty(icalOutput, "X-ZIMBRA-BILLING-INFO", billingInfo, false);
/*     */       
/* 455 */       IcalUtil.addProperty(icalOutput, "X-ZIMBRA-COMPANIES", companies, false);
/*     */       
/* 457 */       IcalUtil.addProperty(icalOutput, "X-ZIMBRA-ACTUAL-WORK-MINS", actualEffort, false);
/*     */       
/* 459 */       IcalUtil.addProperty(icalOutput, "X-ZIMBRA-TOTAL-WORK-MINS", estimatedEffort, false);
/*     */       
/* 461 */       if (this.icalType == ICALENDAR_TYPE.VEVENT) {
/* 462 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-ALLDAYEVENT", isAllDayEvent.booleanValue() ? "TRUE" : "FALSE");
/*     */         
/*     */ 
/* 465 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-BUSYSTATUS", busyStatus, false);
/*     */         
/* 467 */         if (this.method.equals(Method.REQUEST)) {
/* 468 */           IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-INTENDEDSTATUS", intendedBusyStatus, false);
/*     */         }
/*     */         
/*     */ 
/* 472 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-OWNERAPPTID", ownerApptId, false);
/*     */         
/* 474 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-REPLYTIME", schedView.getAppointmentReplyTime(), false);
/*     */         
/* 476 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-OWNER-CRITICAL-CHANGE", ownerCriticalChange, false);
/*     */         
/*     */ 
/* 479 */         Boolean disallowCounter = schedView.isDisallowCounter();
/* 480 */         if (disallowCounter != null) {
/* 481 */           IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-DISALLOW-COUNTER", disallowCounter.booleanValue() ? "TRUE" : "FALSE");
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 486 */       if (reminderSet) {
/* 487 */         addAlarmComponent(icalOutput, schedView.getReminderDelta());
/*     */       }
/* 489 */       icalOutput.endComponent(this.icalType.toString());
/* 490 */       if (recurrenceIdDateTime == null)
/*     */       {
/*     */ 
/* 493 */         addExceptions(icalOutput, this.recurDef, recurrenceTZinfo, sequenceNum.intValue(), ownerApptId, summary, location, isAllDayEvent.booleanValue());
/*     */       }
/*     */       
/*     */ 
/* 497 */       icalOutput.endCalendar();
/* 498 */       conversionSuccessful = true;
/* 499 */       sLog.info("Calendaring TNEF message mapped to ICALENDAR with UID=%s", new Object[] { this.uid });
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 525 */       return conversionSuccessful;
/*     */     }
/*     */     catch (ParserException e)
/*     */     {
/* 501 */       sLog.debug("Unexpected ParserException thrown", e);
/*     */     } catch (URISyntaxException e) {
/* 503 */       sLog.debug("Unexpected URISyntaxException thrown", e);
/*     */     } catch (ParseException e) {
/* 505 */       sLog.debug("Unexpected ParseException thrown", e);
/*     */     } catch (MessagingException e) {
/* 507 */       sLog.debug("Unexpected MessagingException thrown", e);
/*     */     } catch (NegativeArraySizeException e) {
/* 509 */       sLog.debug("Problem decoding TNEF for ICALENDAR", e);
/*     */     } catch (IOException e) {
/* 511 */       sLog.debug("Unexpected IOException thrown", e);
/*     */     } catch (TNEFtoIcalendarServiceException.UnsupportedTnefCalendaringMsgException e) {
/* 513 */       sLog.debug("Unable to map this message to ICALENDAR", e);
/*     */     } catch (TNEFtoIcalendarServiceException e) {
/* 515 */       sLog.debug("Problem encountered mapping this message to ICALENDAR", e);
/*     */     } finally {
/*     */       try {
/* 518 */         if (tnefStream != null) {
/* 519 */           tnefStream.close();
/*     */         }
/*     */       } catch (IOException ioe) {
/* 522 */         sLog.debug("Problem encountered closing TNEF stream", ioe);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addRecurrenceRelatedProps(ContentHandler icalOutput, RecurrenceDefinition recurDef, TimeZoneDefinition tzDef, boolean isAllDayEvent)
/*     */     throws ServiceException, ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/* 535 */     if (recurDef != null) {
/* 536 */       Property recurrenceProp = recurDef.icalRecurrenceProperty(isAllDayEvent, false);
/*     */       
/* 538 */       IcalUtil.addProperty(icalOutput, recurrenceProp);
/* 539 */       for (DateTime exDate : recurDef.getExdates()) {
/* 540 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "EXDATE", exDate, tzDef, isAllDayEvent);
/*     */       }
/*     */       
/* 543 */       for (DateTime rDate : recurDef.getRdates()) {
/* 544 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "RDATE", rDate, tzDef, isAllDayEvent);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addExceptions(ContentHandler icalOutput, RecurrenceDefinition recurDef, TimeZoneDefinition tzDef, int sequenceNum, Integer ownerApptId, String seriesSummary, String seriesLocation, boolean seriesIsAllDay)
/*     */     throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/* 571 */     if ((this.method.equals(Method.REPLY)) || (this.method.equals(Method.CANCEL)) || (this.method.equals(Method.COUNTER)))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/* 576 */       return;
/*     */     }
/* 578 */     if (recurDef == null) {
/* 579 */       return;
/*     */     }
/* 581 */     for (ChangedInstanceInfo cInst : recurDef.getChangedInstances()) {
/* 582 */       EnumSet<ExceptionInfoOverrideFlag> overrideFlags = cInst.getOverrideFlags();
/*     */       
/*     */ 
/* 585 */       if ((overrideFlags != null) && (!overrideFlags.isEmpty()))
/*     */       {
/*     */ 
/* 588 */         icalOutput.startComponent(this.icalType.toString());
/* 589 */         Boolean exceptIsAllDayEvent = cInst.isAllDayEvent();
/* 590 */         if (exceptIsAllDayEvent == null) {
/* 591 */           exceptIsAllDayEvent = Boolean.valueOf(seriesIsAllDay);
/*     */         }
/* 593 */         String exceptSumm = cInst.getSummary();
/* 594 */         if (exceptSumm == null) {
/* 595 */           exceptSumm = seriesSummary;
/*     */         }
/* 597 */         IcalUtil.addProperty(icalOutput, "SUMMARY", exceptSumm, false);
/* 598 */         String exceptLocation = cInst.getLocation();
/* 599 */         if (exceptLocation == null) {
/* 600 */           exceptLocation = seriesLocation;
/*     */         }
/* 602 */         IcalUtil.addProperty(icalOutput, "LOCATION", exceptLocation, false);
/* 603 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "DTSTART", cInst.getStartDate(), tzDef, exceptIsAllDayEvent.booleanValue());
/*     */         
/* 605 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "DTEND", cInst.getEndDate(), tzDef, exceptIsAllDayEvent.booleanValue());
/*     */         
/* 607 */         IcalUtil.addProperty(icalOutput, "UID", this.uid);
/* 608 */         IcalUtil.addPropertyFromUtcTimeAndZone(icalOutput, "RECURRENCE-ID", cInst.getOriginalStartDate(), tzDef, seriesIsAllDay);
/*     */         
/* 610 */         IcalUtil.addProperty(icalOutput, this.dtstamp);
/* 611 */         BusyStatus exceptBusyStatus = cInst.getBusyStatus();
/* 612 */         addStatusProperty(icalOutput, exceptBusyStatus, null);
/* 613 */         addTranspProperty(icalOutput, exceptBusyStatus);
/* 614 */         IcalUtil.addProperty(icalOutput, "SEQUENCE", Integer.valueOf(sequenceNum), false);
/* 615 */         if (this.method.equals(Method.REQUEST)) {
/* 616 */           IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-INTENDEDSTATUS", exceptBusyStatus, false);
/*     */         }
/*     */         
/*     */ 
/* 620 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-OWNERAPPTID", ownerApptId, false);
/*     */         
/* 622 */         IcalUtil.addProperty(icalOutput, "X-MICROSOFT-CDO-ALLDAYEVENT", exceptIsAllDayEvent.booleanValue() ? "TRUE" : "FALSE");
/*     */         
/*     */ 
/* 625 */         addAlarmComponent(icalOutput, cInst.getReminderDelta());
/* 626 */         icalOutput.endComponent(this.icalType.toString());
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addAttendees(ContentHandler icalOutput, MimeMessage mimeMsg, PartStat partstat, boolean replyWanted)
/*     */     throws ParserException, URISyntaxException, IOException, ParseException, MessagingException
/*     */   {
/* 646 */     InternetAddress firstFromIA = null;
/* 647 */     String firstFromEmailAddr = null;
/* 648 */     String senderMailto = null;
/* 649 */     String senderCn = null;
/*     */     
/* 651 */     Address[] toRecips = null;
/* 652 */     Address[] ccRecips = null;
/* 653 */     Address[] bccRecips = null;
/* 654 */     Address[] msgFroms = null;
/* 655 */     Address msgSender = null;
/* 656 */     if (mimeMsg != null) {
/* 657 */       toRecips = mimeMsg.getRecipients(Message.RecipientType.TO);
/* 658 */       ccRecips = mimeMsg.getRecipients(Message.RecipientType.CC);
/* 659 */       bccRecips = mimeMsg.getRecipients(Message.RecipientType.BCC);
/* 660 */       msgFroms = mimeMsg.getFrom();
/* 661 */       msgSender = mimeMsg.getSender();
/*     */     }
/* 663 */     if (msgFroms != null) {
/* 664 */       if (msgFroms.length != 1) {
/* 665 */         sLog.debug(msgFroms.length + " From: recipients for " + this.method.getValue());
/*     */       }
/* 667 */       if (msgFroms.length >= 1) {
/* 668 */         firstFromIA = (InternetAddress)msgFroms[0];
/* 669 */         firstFromEmailAddr = firstFromIA.getAddress();
/*     */       }
/* 671 */       if (msgSender != null) {
/* 672 */         String senderAddr = msgSender.toString();
/* 673 */         if ((msgSender instanceof InternetAddress)) {
/* 674 */           InternetAddress senderIA = (InternetAddress)msgSender;
/* 675 */           senderAddr = senderIA.getAddress();
/* 676 */           senderCn = senderIA.getPersonal();
/* 677 */           if (!firstFromIA.equals(senderIA)) {
/* 678 */             senderMailto = "Mailto:" + senderAddr;
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 684 */     if ((this.method.equals(Method.REPLY)) || (this.method.equals(Method.COUNTER)))
/*     */     {
/* 686 */       if (toRecips != null) {
/* 687 */         if (toRecips.length != 1) {
/* 688 */           sLog.debug(toRecips.length + " To: recipients for " + this.method.getValue());
/*     */         }
/* 690 */         if (toRecips.length >= 1) {
/* 691 */           InternetAddress ia = (InternetAddress)toRecips[0];
/* 692 */           String email = ia.getAddress();
/* 693 */           String displayName = ia.getPersonal();
/* 694 */           icalOutput.startProperty("ORGANIZER");
/* 695 */           icalOutput.propertyValue("Mailto:" + email);
/* 696 */           if (displayName != null) {
/* 697 */             icalOutput.parameter("CN", displayName);
/*     */           }
/* 699 */           icalOutput.endProperty("ORGANIZER");
/*     */         }
/*     */       }
/* 702 */       if (firstFromEmailAddr != null) {
/* 703 */         String displayName = firstFromIA.getPersonal();
/* 704 */         icalOutput.startProperty("ATTENDEE");
/* 705 */         icalOutput.propertyValue("Mailto:" + firstFromEmailAddr);
/* 706 */         if (displayName != null) {
/* 707 */           icalOutput.parameter("CN", displayName);
/*     */         }
/* 709 */         icalOutput.parameter("CUTYPE", CuType.INDIVIDUAL.getValue());
/* 710 */         if (partstat != null) {
/* 711 */           icalOutput.parameter("PARTSTAT", partstat.getValue());
/*     */         }
/* 713 */         if (senderMailto != null) {
/* 714 */           icalOutput.parameter("SENT-BY", senderMailto);
/*     */         }
/* 716 */         icalOutput.endProperty("ATTENDEE");
/*     */       }
/*     */     }
/*     */     else {
/* 720 */       InternetAddress organizerEmail = null;
/* 721 */       if (firstFromEmailAddr != null) {
/* 722 */         SentBy sentBy = null;
/* 723 */         Cn cn = null;
/* 724 */         if (senderMailto != null) {
/* 725 */           sentBy = new SentBy(senderMailto);
/*     */         }
/* 727 */         organizerEmail = firstFromIA;
/* 728 */         String displayName = firstFromIA.getPersonal();
/* 729 */         if ((displayName != null) && (!displayName.equals(firstFromEmailAddr))) {
/* 730 */           cn = new Cn(displayName);
/*     */         }
/* 732 */         Organizer organizer = new Organizer();
/* 733 */         organizer.setValue("Mailto:" + firstFromEmailAddr);
/* 734 */         if (cn != null) {
/* 735 */           organizer.getParameters().add(cn);
/*     */         }
/* 737 */         if (sentBy != null) {
/* 738 */           organizer.getParameters().add(sentBy);
/*     */         }
/* 740 */         IcalUtil.addProperty(icalOutput, organizer);
/* 741 */         if (this.icalType == ICALENDAR_TYPE.VEVENT)
/*     */         {
/* 743 */           Attendee attendee = new Attendee("Mailto:" + firstFromEmailAddr);
/* 744 */           if (cn != null) {
/* 745 */             attendee.getParameters().add(cn);
/*     */           }
/* 747 */           attendee.getParameters().add(CuType.INDIVIDUAL);
/* 748 */           attendee.getParameters().add(Role.REQ_PARTICIPANT);
/* 749 */           if (!this.method.equals(Method.CANCEL)) {
/* 750 */             PartStat orgPartstat = PartStat.ACCEPTED;
/* 751 */             if (ccRecips != null) {
/* 752 */               for (Address a : ccRecips) {
/* 753 */                 InternetAddress ia = (InternetAddress)a;
/* 754 */                 if (organizerEmail.equals(ia)) {
/* 755 */                   orgPartstat = PartStat.TENTATIVE;
/* 756 */                   break;
/*     */                 }
/*     */               }
/*     */             }
/* 760 */             attendee.getParameters().add(orgPartstat);
/*     */           }
/*     */           
/*     */ 
/* 764 */           IcalUtil.addProperty(icalOutput, attendee);
/*     */         }
/*     */       }
/* 767 */       if (toRecips != null) {
/* 768 */         for (Address a : toRecips) {
/* 769 */           InternetAddress ia = (InternetAddress)a;
/* 770 */           if ((organizerEmail == null) || (!organizerEmail.equals(ia)))
/*     */           {
/*     */ 
/* 773 */             addAttendee(icalOutput, ia, Role.REQ_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
/*     */           }
/*     */         }
/*     */       }
/* 777 */       if (ccRecips != null) {
/* 778 */         for (Address a : ccRecips) {
/* 779 */           InternetAddress ia = (InternetAddress)a;
/* 780 */           if ((organizerEmail == null) || (!organizerEmail.equals(ia)))
/*     */           {
/*     */ 
/* 783 */             addAttendee(icalOutput, ia, Role.OPT_PARTICIPANT, CuType.INDIVIDUAL, partstat, replyWanted);
/*     */           }
/*     */         }
/*     */       }
/* 787 */       if (bccRecips != null) {
/* 788 */         for (Address a : bccRecips) {
/* 789 */           InternetAddress ia = (InternetAddress)a;
/* 790 */           addAttendee(icalOutput, ia, Role.NON_PARTICIPANT, CuType.RESOURCE, partstat, replyWanted);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 795 */     if (senderMailto != null) {
/* 796 */       XProperty msOlkSender = new XProperty("X-MS-OLK-SENDER", senderMailto);
/* 797 */       if (senderCn != null) {
/* 798 */         Cn cn = new Cn(senderCn);
/* 799 */         msOlkSender.getParameters().add(cn);
/*     */       }
/* 801 */       IcalUtil.addProperty(icalOutput, msOlkSender);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addAttendee(ContentHandler icalOutput, InternetAddress ia, Role role, CuType cuType, PartStat partstat, boolean rsvp)
/*     */     throws URISyntaxException, ParserException, IOException, ParseException
/*     */   {
/* 822 */     String email = ia.getAddress();
/* 823 */     String displayName = ia.getPersonal();
/* 824 */     Attendee attendee = new Attendee("Mailto:" + email);
/* 825 */     if ((displayName != null) && (!displayName.equals(email))) {
/* 826 */       Cn cn = new Cn(displayName);
/* 827 */       attendee.getParameters().add(cn);
/*     */     }
/* 829 */     if (rsvp) {
/* 830 */       attendee.getParameters().add(Rsvp.TRUE);
/*     */     }
/* 832 */     attendee.getParameters().add(role);
/* 833 */     attendee.getParameters().add(cuType);
/* 834 */     if (partstat != null) {
/* 835 */       attendee.getParameters().add(partstat);
/*     */     }
/* 837 */     IcalUtil.addProperty(icalOutput, attendee);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void addAlarmComponent(ContentHandler icalOutput, Integer reminderDelta)
/*     */     throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/* 851 */     if (reminderDelta == null) {
/* 852 */       return;
/*     */     }
/*     */     
/* 855 */     icalOutput.startComponent("VALARM");
/* 856 */     IcalUtil.addProperty(icalOutput, Action.DISPLAY);
/* 857 */     IcalUtil.addProperty(icalOutput, "DESCRIPTION", "Reminder", false);
/*     */     String trigStr;
/* 859 */     String trigStr; if (reminderDelta.intValue() % 60 == 0) {
/* 860 */       reminderDelta = Integer.valueOf(reminderDelta.intValue() / 60);
/* 861 */       String trigStr; if (reminderDelta.intValue() % 24 == 0) {
/* 862 */         reminderDelta = Integer.valueOf(reminderDelta.intValue() / 24);
/* 863 */         trigStr = String.format("-PT%dD", new Object[] { reminderDelta });
/*     */       } else {
/* 865 */         trigStr = String.format("-PT%dH", new Object[] { reminderDelta });
/*     */       }
/*     */     } else {
/* 868 */       trigStr = String.format("-PT%dM", new Object[] { reminderDelta });
/*     */     }
/* 870 */     ParameterList trigParams = new ParameterList();
/* 871 */     trigParams.add(Related.START);
/* 872 */     Trigger trigger = new Trigger(trigParams, trigStr);
/* 873 */     IcalUtil.addProperty(icalOutput, trigger);
/* 874 */     icalOutput.endComponent("VALARM");
/*     */   }
/*     */   
/*     */   private void addTranspProperty(ContentHandler icalOutput, BusyStatus busyStatus) throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/* 879 */     if ((this.method.equals(Method.REPLY)) || (this.method.equals(Method.CANCEL)) || (this.method.equals(Method.COUNTER)))
/*     */     {
/*     */ 
/*     */ 
/* 883 */       return;
/*     */     }
/* 885 */     if ((busyStatus != null) && (busyStatus.equals(BusyStatus.FREE))) {
/* 886 */       IcalUtil.addProperty(icalOutput, Transp.TRANSPARENT);
/*     */     } else {
/* 888 */       IcalUtil.addProperty(icalOutput, Transp.OPAQUE);
/*     */     }
/*     */   }
/*     */   
/*     */   private void addStatusProperty(ContentHandler icalOutput, BusyStatus busyStatus, EnumSet<MeetingTypeFlag> meetingTypeFlags)
/*     */     throws ParserException, URISyntaxException, IOException, ParseException
/*     */   {
/* 895 */     if (this.icalType.equals(ICALENDAR_TYPE.VTODO)) {
/* 896 */       return;
/*     */     }
/* 898 */     Status icalStatus = null;
/* 899 */     if (this.method.equals(Method.REQUEST)) {
/* 900 */       if (busyStatus == null) {
/* 901 */         return;
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 906 */       if (busyStatus.equals(BusyStatus.TENTATIVE)) {
/* 907 */         icalStatus = Status.VEVENT_TENTATIVE;
/*     */       } else {
/* 909 */         icalStatus = Status.VEVENT_CONFIRMED;
/*     */       }
/*     */     }
/* 912 */     else if (this.method.equals(Method.CANCEL))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/* 917 */       if ((busyStatus == null) || (busyStatus.equals(BusyStatus.FREE))) {
/* 918 */         if ((meetingTypeFlags != null) && (meetingTypeFlags.contains(MeetingTypeFlag.MTG_INFO)))
/*     */         {
/* 920 */           sLog.debug("STATUS:CANCELLED suppressed - PidLidMeetingType has mtgInfo flag set");
/*     */         } else {
/* 922 */           icalStatus = Status.VEVENT_CANCELLED;
/*     */         }
/*     */       }
/*     */     }
/* 926 */     if (icalStatus != null) {
/* 927 */       IcalUtil.addProperty(icalOutput, icalStatus);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public RecurrenceDefinition getRecurDef()
/*     */   {
/* 935 */     return this.recurDef;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/tnef/DefaultTnefToICalendar.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */